package com.hzit.order.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.hzit.order.config.OrderConfig;
import com.hzit.order.service.OrderService;
import com.hzit.paycommon.paycommonapi.req.PayReq;
import com.hzit.paycommon.paycommonapi.utils.PayDigestUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.Date;

@Service
public class OderServiceImpl implements OrderService {

    @Autowired
    private OrderConfig orderConfig;


    @Autowired
    private RestTemplate restTemplate;



    @Override
    public String createOrder(String name) {

        //1.封装支付接口请求的参数
        PayReq payReq = new PayReq();
        payReq.setUserId("test123");
        payReq.setBody("测试订单");
        payReq.setMchOrderNo(String.valueOf(System.currentTimeMillis())); //分布式唯一id
        payReq.setAmount("100"); //单位分
        payReq.setOverTime(new Date());
        payReq.setSuject("测试商品");
        payReq.setPayWay(name);

        //2.获取签名的密钥
        String reqKey = orderConfig.getReqKey();

        //3.生成sign值
        JSONObject map = (JSONObject) JSONObject.toJSON(payReq);
        String sign = PayDigestUtil.getSign(map,reqKey);

        System.out.println("-------sign-" + sign);

        payReq.setSign(sign);

        //调用支付服务的支付接口。

        String url = "http://127.0.0.1:1111/pay/create";

        String xx = restTemplate.postForObject(url,payReq,String.class);

        System.out.println("=============xx======"+ xx);

        return xx;
    }
}
